package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Dealing;
import org.csu.secondhand.api.service.DealingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dealing")
public class DealingController {
    @Autowired
    DealingService dealingService;

    @PostMapping("insertDealing")
    @ResponseBody
    public Map<Object, Object> insertDealing(String username,
                                             String content,
                                             String photo,
                                             int type,
                                             double price,
                                             String exchange,
                                             String category,
                                             int status,
                                             String itemname) {
        Map<Object, Object> map = new HashMap<>();
        Dealing dealing = dealingService.insertDealing(
                new Dealing(0,
                        TimeUtil.getDate(),
                        username,
                        content,
                        photo,
                        type,
                        price,
                        exchange,
                        category,
                        status,
                        itemname));
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealing);
        return map;
    }

    @PostMapping("deleteDealing")
    @ResponseBody
    public Map<Object, Object> deleteDealing(long id) {
        Map<Object, Object> map = new HashMap<>();
        Dealing dealing = dealingService.deleteDealing(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealing);
        return map;
    }

    @PostMapping("editDealing")
    @ResponseBody
    public Map<Object, Object> editDealing(long id,
                                           String username,
                                           String content,
                                           String photo,
                                           int type,
                                           double price,
                                           String exchange,
                                           String category,
                                           int status,
                                           String itemname) {
        Map<Object, Object> map = new HashMap<>();
        Dealing dealing = dealingService.editDealing(
                new Dealing(id,
                        TimeUtil.getDate(),
                        username,
                        content,
                        photo,
                        type,
                        price,
                        exchange,
                        category,
                        status,
                        itemname));
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealing);
        return map;
    }

    @GetMapping("getAllDealing")
    @ResponseBody
    public Map<Object, Object> getAllDealing() {
        Map<Object, Object> map = new HashMap<>();
        List<Dealing> dealingList = dealingService.getAllDealing();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealingList);
        return map;
    }


    @GetMapping("searchDealing")
    @ResponseBody
    public Map<Object, Object> searchDealing(String keyword) {
        Map<Object, Object> map = new HashMap<>();
        List<Dealing> dealingList = dealingService.searchDealing(keyword);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealingList);
        return map;
    }

    @GetMapping("getDealingByUser")
    @ResponseBody
    public Map<Object, Object> getDealingByUser(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Dealing> dealingList = dealingService.getDealingByUser(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealingList);
        return map;
    }

    @GetMapping("getDealingById")
    @ResponseBody
    public Map<Object, Object> getDealingById(long id) {
        Map<Object, Object> map = new HashMap<>();
        Dealing dealing = dealingService.getDealingById(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", dealing);
        return map;
    }

}
